package com.bmit.ava.controller;

import com.bmit.ava.model.AvaCodeWithBLOBs;
import com.bmit.ava.service.CodeService;
import com.bmit.ava.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("code")
public class CodeController {

    @Autowired
    private CodeService codeService;

    @RequestMapping("/create")
    public boolean createNewCode(@RequestParam String title, @RequestParam String labels, @RequestParam String resume, @RequestParam String code) {
        AvaCodeWithBLOBs avaCodeWithBLOBs = new AvaCodeWithBLOBs();
        avaCodeWithBLOBs.setTitle(title);
        avaCodeWithBLOBs.setLabels(labels);
        avaCodeWithBLOBs.setResume(resume);
        avaCodeWithBLOBs.setCode(code);
        avaCodeWithBLOBs.setVersion(1);
        avaCodeWithBLOBs.setLastUpdateDate(TimeUtils.TimeToDateString(new Date()));
        return codeService.createNewCode(avaCodeWithBLOBs);
    }

    @RequestMapping( "/show-one")
    public AvaCodeWithBLOBs get(@RequestParam int codeId) {
        AvaCodeWithBLOBs avaCodeWithBLOBs = codeService.selectOneById(codeId);
        return avaCodeWithBLOBs;
    }
}
